package com.paradisecloudtech.vc.system.ui.activity;

import android.os.SystemClock;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.paradisecloudtech.vc.system.entity.Constant;
import com.paradisecloudtech.vc.system.R;
import com.common.library.base.BaseActivity;
import com.common.library.Utils.ActivityUtil;
import com.common.library.Utils.SpUtil;
import com.common.library.Utils.UIUtil;

import butterknife.BindView;

public class SplashActivity extends BaseActivity {

    private Long mTime;

    @BindView(R.id.iv_splash_loading)
    public ImageView ivSplashLoading;

    @BindView(R.id.rl_splash_root)
    public RelativeLayout rlSplashRoot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_splash;
    }

    @Override
    public void intVar() {
        //让Splash的logo和activity的window的windowBackground对齐
        //window的android:gravity="center",和rootView的android:gravity="center"不一样呢
        //window的高度和DecorView的高度一样，都是屏幕的高度,包括导航栏高度+状态栏高度（但是状态栏不属于decorView）+ActionBar+rootView
        //rootView的高度为内容高度,就是splash_activity.xml里面的内容，
        //
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) rlSplashRoot.getLayoutParams();
        params.topMargin = UIUtil.getStatusBarHeight();
        rlSplashRoot.setLayoutParams(params);

    }

    @Override
    public void initData() {
        mTime = SpUtil.getLong(Constant.TIME, 0);
        if (mTime != 0 && System.currentTimeMillis() - mTime < 7 * 24 * 60 * 60 * 1000) {
            startActivity(MainActivity.class);
        } else if (mTime == 0) {
            startActivity(LoginActivity.class);
        } else if (System.currentTimeMillis() - mTime > 7 * 24 * 60 * 60 * 1000) {
            Toast.makeText(mContext, "超过有效期，请重新登录", Toast.LENGTH_SHORT).show();
            startActivity(LoginActivity.class);
        }
    }

    @Override
    public void initListener() {
        Animation loadAnimation = AnimationUtils.loadAnimation(mContext, R.anim.login_activity_loginloading);
        ivSplashLoading.setAnimation(loadAnimation);
    }

    private void startActivity(final Class cls) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(1000);
                ActivityUtil.startActivity(mContext, cls);
                finish();
            }
        }).start();
    }
}
